# 检查是否以管理员权限运行
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Warning "Execute With Administrator!!!"
    exit
}

# 定义下载链接和目标路径
$url = "https://vip.123pan.cn/1843306915/DirectLink/microsoft/powershell/aria2c.exe"
$baseDir = "C:\Portable"
$installPath = "$baseDir\aria2c"
$exePath = "$installPath\aria2c.exe"
$downloadPath = "$env:TEMP\aria2c.exe"

# 创建目标目录（如果不存在）
if (-not (Test-Path -Path $installPath)) {
    New-Item -ItemType Directory -Path $installPath | Out-Null
    Write-Host "Created directory: $installPath"
}

# 如果 aria2c.exe 已存在，先删除
if (Test-Path -Path $exePath) {
    Remove-Item -Path $exePath -Force
    Write-Host "Removed existing aria2c.exe"
}

# 下载文件
Write-Host "Downloading aria2c.exe..."
try {
    $ProgressPreference = 'SilentlyContinue'  # 禁用进度条以提升下载速度
    Invoke-WebRequest -Uri $url -OutFile $downloadPath -UseBasicParsing
}
finally {
    $ProgressPreference = 'Continue'  # 恢复进度条显示
}

# 移动文件到安装目录
Move-Item -Path $downloadPath -Destination $exePath -Force
Write-Host "Moved aria2c.exe to $exePath"

# 添加到系统 PATH
$currentPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
if ($currentPath -notlike "*$installPath*") {
    $newPath = "$currentPath;$installPath"
    [Environment]::SetEnvironmentVariable("Path", $newPath, [EnvironmentVariableTarget]::Machine)
    Write-Host "Added $installPath to system PATH"
}

Write-Host "Installation completed successfully!"

